﻿using System;
using System.Collections.Generic;
using Opalis.QuickIntegrationKit;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Collections;

namespace Active_Directory
{
    [OpalisObject("Unlock Account", ShowFilters = false)]
    class UnLockAccount : IOpalisObject
    {
        private ConnectionCredentials credentials;

        [OpalisConfiguration]
        public ConnectionCredentials Credentials
        {
            get { return credentials; }
            set { credentials = value; }
        }

        public void Design(IOpalisDesigner designer)
        {
            designer.AddInput("Object LDAP Path").WithDefaultValue("LDAP://CN=ObjectName,OU=Container,DC=Contoso,DC=Com");
            designer.AddOutput("Unlocked Object LDAP Path").AsString();
        }

        public void Execute(IOpalisRequest request, IOpalisResponse response)
        {
            string objectLDAPPath = request.Inputs["Object LDAP Path"].AsString();

            DirectoryEntry obj = new DirectoryEntry(objectLDAPPath, credentials.UserName + "@" + credentials.Domain, credentials.Password);
            obj.Properties["LockOutTime"].Value = 0; //unlock account

            obj.CommitChanges();
            obj.Close();

            response.Publish("Unlocked Object LDAP Path", objectLDAPPath);
        }
    }
}
